package com.cz.nongyetongv5.activity;

import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.PhotoDetailPageAdapter;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.Photo;
import com.cz.nongyetongv5.custom.dialog.CustomDialog;
import com.cz.nongyetongv5.custom.photoview.PhotoViewAttacher.OnPhotoTapListener;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class RecordHistoryPhotoDetailActivity extends Activity implements
		OnClickListener {

	/**
	 * 返回按钮
	 */
	private ViewGroup btnBack = null;

	private com.cz.nongyetongv5.custom.photoview.PhotoView ivImg = null;

	private String imgurl = null;

	private ViewGroup rlTitleBar = null;

	@Override
	protected void onDestroy() {
		InitApplication initApplication = (InitApplication) getApplicationContext();
		initApplication.getActivities().remove(this);

		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.recordhistory_photo_detail);

		rlTitleBar = (ViewGroup) findViewById(R.id.rlTitleBar);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		ivImg = (com.cz.nongyetongv5.custom.photoview.PhotoView) findViewById(R.id.ivImg);

		if (!CommonConstants.isNetworkConnected(this)) {

			Toast.makeText(this,
					getResources().getString(R.string.error_no_network),
					Toast.LENGTH_SHORT).show();

			finish();

			return;
		}

		imgurl = getIntent().getStringExtra("imgurl");

		loadRightTitle();

		new AsyncTask<String, String, Bitmap>() {

			private CustomDialog customDialog = null;

			protected void onPostExecute(Bitmap result) {

				ivImg.setImageDrawable(new BitmapDrawable(result));

				customDialog.dismiss();

			};

			protected void onPreExecute() {
				customDialog = new CustomDialog(
						RecordHistoryPhotoDetailActivity.this);

				customDialog.setMessage("正在加载..");
				customDialog.showWait();
			};

			@Override
			protected Bitmap doInBackground(String... params) {

				return getHttpBitmap(imgurl);
			}

		}.execute();

		ivImg.setOnPhotoTapListener(new OnPhotoTapListener() {

			@Override
			public void onPhotoTap(View view, float x, float y) {

				if (rlTitleBar.getVisibility() == View.VISIBLE) {
					rlTitleBar.setVisibility(View.GONE);
				} else {
					rlTitleBar.setVisibility(View.VISIBLE);
				}

			}
		});

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {

					GoHomeUtil.goHome(getApplicationContext());

				}

				pop.dismiss();
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	/**
	 * 当按下back案件的时候
	 */
	@Override
	public void onBackPressed() {

		super.onBackPressed();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		if (v == btnBack) {
			onBackPressed();
		}

	}

	public static Bitmap getHttpBitmap(String url) {
		URL myFileURL;
		Bitmap bitmap = null;
		try {
			myFileURL = new URL(url);
			// 获得连接
			HttpURLConnection conn = (HttpURLConnection) myFileURL
					.openConnection();
			// 设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
			conn.setConnectTimeout(6000);
			// 连接设置获得数据流
			conn.setDoInput(true);
			// 不使用缓存
			conn.setUseCaches(false);
			// 这句可有可无，没有影响
			conn.connect();
			// 得到数据流
			InputStream is = conn.getInputStream();
			// 解析得到图片
			bitmap = BitmapFactory.decodeStream(is);
			// 关闭数据流
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;

	}

}
